package com.tms.app.tms.ui.base.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tms.app.tms.R;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.ErrorMsgRes;
import com.tms.app.tms.ui.base.ui.AbsFragment;
import com.tms.app.tms.ui.login.LoginAt;
import com.tms.app.tms.ui.setting.SettingHomeActivity;
import com.tms.app.tms.utils.SharePreferenceUtil;
import com.tms.app.tms.widget.MyDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Unbinder;

/**
 * Created by happy on 2017/8/26.
 */

public abstract class BaseFragment extends AbsFragment {
    public Unbinder unbinder;
    public List list=new ArrayList<>();
    public View rootView;
    private MyDialog loadingDialog;
    public String token;
    private TextView toolbarRight2;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        token= SharePreferenceUtil.getToken(getContext());
    }

    //    @Override
//    public void onResume() {
//        super.onResume();
//        EventBus.getDefault().register(this);
//    }
//
//    @Override
//    public void onStop() {
//        super.onStop();
//        EventBus.getDefault().unregister(this);
//    }

    public void showMsg(String msg) {
        Toast.makeText(getContext(),msg, Toast.LENGTH_SHORT).show();
    }

    public TextView toolbarLeft;
    public TextView toolbarCenter;
    public TextView toolbarRight;

    private  void  initViewToolBar(View view){
        try {
            toolbarCenter= (TextView)view. findViewById(R.id.toolbar_center);
            toolbarLeft= (TextView)view.  findViewById(R.id.toolbar_left);
            toolbarRight= (TextView) view. findViewById(R.id.toolbar_right);
            toolbarRight2= (TextView)  view. findViewById(R.id.toolbar_right_2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setToolBarTitle(View view,String title,int left,int right){
        if (toolbarLeft==null||toolbarRight==null||toolbarCenter==null)
            initViewToolBar(view);
        if (toolbarCenter!=null)
            toolbarCenter.setText(title);
        if (toolbarLeft!=null&&left!=-1){
            if (left==0)
                toolbarLeft.setVisibility(View.GONE);
            else
                toolbarLeft.setBackgroundResource(left);
        }

        if (toolbarRight!=null&&right!=-1){
            if (right==0){
                toolbarRight.setVisibility(View.INVISIBLE);
                toolbarRight2.setVisibility(View.INVISIBLE);
            }else {
                toolbarRight2.setBackgroundResource(right);
                toolbarRight2.setVisibility(View.VISIBLE);
                toolbarRight.setVisibility(View.INVISIBLE);
            }
        }
    }

    public void setToolBarLisenter(View view,View.OnClickListener listener,View.OnClickListener listener2){
        if (toolbarLeft==null||toolbarRight==null||toolbarCenter==null)
            initViewToolBar(view);

        if (toolbarLeft!=null)
            toolbarLeft.setOnClickListener(listener);
        if (toolbarRight!=null)
            toolbarRight.setOnClickListener(listener2);
        if (toolbarRight2!=null)
            toolbarRight2.setOnClickListener(listener2);
    }







    @Subscribe(threadMode = ThreadMode.MAIN)
    public <T extends BaseResponse> void  checkResponseBase(T t){
        if (t instanceof ErrorMsgRes){
            ErrorMsgRes errorMsgRes= (ErrorMsgRes) t;
            handlerErrorMsg(errorMsgRes.getCode());
        }
        if (t instanceof BaseResponse)
        {
            switch (t.getCode()){
                case 200:
                    break;
                case 404:
                    showNetError();
                    break;
                case 3001:
                   // showLoadingDialog();
                    break;
                case 5001:
                    hideLoadingDialog();
                    break;
                case 4045:
                case 4046:
                case 10012:
                    showMsg("token失效，该账号或在其他机器登陆");
                    Intent intent = new Intent(getContext(), LoginAt.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    getActivity().finish();
                    break;
                case 6001:
                    // showMsg("请求服务器错误");
                    hideLoadingDialog();
                    //   Snackbar.make()
                    showMsg("请求超时，请检查网络后重试");
                    break;
            }
        }
    }


    public void showNetError(){
        showNetError(0);
    }
    public void showNetError(int imglevel){
        showNetError(imglevel,"网络异常");
    }
    public void showNetError(int imglevel,String msg){
       showNetError(-1,imglevel,msg);
    }
    public void showNetError(int color,int imglevel,String msg){
        try {
           rootView. findViewById(R.id.net_errot_layout).setVisibility(View.VISIBLE);
            ImageView imageView=rootView.findViewById(R.id.img_net_error);
            TextView textView=rootView.findViewById(R.id.text_net_error);
            imageView.getBackground().setLevel(imglevel);
            if (color!=-1)
                textView.setTextColor(color);
            textView.setText(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideNerError(){
        try {
            rootView. findViewById(R.id.net_errot_layout).setVisibility(View.GONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideSoftInputFromWindow(Context context, View view){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }


    public abstract void handlerErrorMsg(int errorCode);

    public void showLoadingDialog(){
        showLoadingDialog(" 正在加载请稍候");
    }
    public void showLoadingDialog(String title){
        if (loadingDialog!=null&&loadingDialog.isShowing())
            return;
        loadingDialog= new MyDialog( getContext(),title);
        loadingDialog.show();
    }

    public void hideLoadingDialog(){
        if (loadingDialog!=null)
            loadingDialog.close();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try {
            unbinder.unbind();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
